
#include "ZhouBian_ui.h"
#include <QApplication>
#include <QVBoxLayout>
#include <QLabel>
#include <QPixmap>
#include <QWidget>
#include <QMessageBox>
#include <QTimer>

ProductWidget::ProductWidget(QWidget *parent)
    : QWidget{parent}
{
    setupUi();
}
void ProductWidget::setupUi()
{
    // Main horizontal layout for the entire product item
    QHBoxLayout *mainLayout = new QHBoxLayout(this);
    mainLayout->setSpacing(10);

    // Container for the left-side layout (image)
    imageLabel = new QLabel(this);
    imageLabel->setFixedSize(250, 280);

    mainLayout->addWidget(imageLabel, 1);  // Add image with stretch factor

    // Right-side layout for title, price, and description
    QVBoxLayout *detailsLayout = new QVBoxLayout();
    titleLabel = new QLabel(this);
    titleLabel->setStyleSheet("font-size: 18px; font-weight: bold;");
    detailsLayout->addWidget(titleLabel);

    // Increase the font size of priceLabel
    priceLabel = new QLabel(this);
    priceLabel->setStyleSheet("color: red; font-size: 20px; font-weight: bold;");  // Larger font for price
    detailsLayout->addWidget(priceLabel);

    descriptionLabel = new QLabel(this);
    descriptionLabel->setWordWrap(true); // Allow text to wrap
    detailsLayout->addWidget(descriptionLabel);

    detailsLayout->setSpacing(5);  // Space between title, price, and description

    // Add the right-side layout (details) to the main layout
    mainLayout->addLayout(detailsLayout, 2);  // Stretch factor for details

    // Set main layout margins and spacing
    mainLayout->setContentsMargins(0, 0, 0, 0);

    // Outer layout to hold the main content and separator
    QVBoxLayout *outerLayout = new QVBoxLayout(this);
    outerLayout->addLayout(mainLayout);

    // Add a horizontal separator line after the product details
    QFrame *separatorLine = new QFrame(this);
    separatorLine->setFrameShape(QFrame::HLine);
    separatorLine->setFrameShadow(QFrame::Sunken);
    separatorLine->setFixedWidth(1620);  // Adjust as per your layout width
    outerLayout->addWidget(separatorLine);  // Add separator to outer layout, which separates items

    outerLayout->setContentsMargins(0, 0, 0, 0);
    outerLayout->setSpacing(10);

    // Set the final layout for the widget
    setLayout(outerLayout);

    // Set size constraints
    setFixedHeight(300);  // Set fixed height for the widget
    setMouseTracking(true);
}

void ProductWidget::setEvent(const QPixmap &image, const QString &title, const QString &price, const QString &description, int productId)
{
    this->productId = productId;

    // Resize and set image with smooth transformation
    QPixmap scaledImage = image.scaled(imageLabel->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
    imageLabel->setPixmap(scaledImage);

    // Set text for title, price, and description
    titleLabel->setText(title);
    priceLabel->setText("售价 " + price);
    descriptionLabel->setText(description);
}

void ProductWidget::onBuyButtonClicked()
{
    emit buyButtonClicked(titleLabel->text(), priceLabel->text(), productId); // 发送名称、价格和商品ID
}

void ProductWidget::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        emit productClicked(imageLabel->pixmap().copy(), titleLabel->text(), priceLabel->text(), descriptionLabel->text(), productId);
    }
}
ProductWidget::~ProductWidget() {}

